package com.nf.health.app.utils;

import android.content.Context;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.TextAppearanceSpan;
import android.widget.TextView;

import com.nf.health.app.R;

import java.util.ArrayList;

/**
 * Created by 恩福 on 2016/10/13.
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class TextUtilTools {
    /**
     * 将字符串中的数字高亮显示 用法 xx.setText(changeNum(xx,xx,xx));
     * @param content
     * @return
     */
    public static SpannableStringBuilder changeNum(Context context,String content,int style){
        ArrayList<Integer> positions = StringUtil.getNumPosition(content);
        SpannableStringBuilder sb= new SpannableStringBuilder(content);
        for (int i = 0; i < positions.size(); i++) {
            sb.setSpan(new TextAppearanceSpan(context, style), positions.get(i), positions.get(i) + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
        }
        return sb;
    }

    /**
     * 给TextView设置html样式的文字
     * @param textView
     * @param htmlText
     */
    public static void setTextFromHtml(TextView textView,String htmlText){
        CharSequence charSequenced = Html.fromHtml(htmlText);
        textView.setText(charSequenced);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}
